package com.zwr.sheller.customer.Service.Impl;

import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.common.LSYExceptionCode;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.core.util.SnowflakeUtil;
import com.zwr.sheller.customer.Service.RemarkService;
import com.zwr.sheller.customer.mapper.OrderRemarkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author LSY
 * @Date 2022/1/13 15:58
 * @Version 1.0
 **/
@Service
public class RemarkServiceImpl implements RemarkService {

    OrderRemarkMapper orderRemarkMapper;
    RedisTemplate<String, Object> template;
    ValueOperations<String, Object> stringOps;
    @Autowired(required = false)
    public RemarkServiceImpl(OrderRemarkMapper orderRemarkMapper, RedisTemplate<String, Object> template) {
        this.orderRemarkMapper = orderRemarkMapper;
        this.template = template;
        stringOps = template.opsForValue();
    }

    @Override
    public List<Map<String, Object>> userFindRemark(Long oid) throws LSYBusinessException {
        Set<String> keys = template.keys("remark:*" + oid + "*");

        List<Map<String, Object>> remarks = new ArrayList<>();

        if(keys != null) {
            for (String key : keys) {
                Map<String, Object> map = JsonUtil.stringToObject(String.valueOf(stringOps.get(key)), Map.class);
                map.remove("uid");
                map.remove("isAudit");
                map.remove("isReply");
                map.remove("storeName");

                remarks.add(map);
            }
        }
        else {
            throw new LSYBusinessException(LSYExceptionCode.NO_REMARK);
        }

        return remarks;
    }

    @Override
    public void userComment(Long oid, Long uid, Long sid, Integer rank, String content, List<String> images) throws LSYBusinessException {
        String time = new Timestamp(System.currentTimeMillis()).toString();

        Map<String, Object> info = orderRemarkMapper.findGoodsSkuById(sid, oid);
        Map<String, Object> map = new HashMap<>();

        for (int i = 0; i < images.size(); i++) {
            String source = images.get(i).substring(27);
            String dest = "remark" + images.get(i).substring(36);
            OSSUtil.moveFile(source, dest);

            images.set(i, dest);
        }

        map.put("uid", uid);
        map.put("goodsImg", info.get("goodsImg"));
        map.put("skuNum", info.get("skuNum"));
        map.put("isAudit", false);
        map.put("rank", rank);
        map.put("isReply", false);
        map.put("storeName", info.get("storeName"));
        map.put("goodsName", info.get("goodsName"));
        map.put("skuName", info.get("skuName"));
        map.put("content", content);
        map.put("images", images);
        map.put("time", time);

        stringOps.set(
                "remark:" +
                        info.get("gid") +
                        ":" +
                        oid +
                        ":" +
                        info.get("storeId") +
                        ":" +
                        time +
                        ":" +
                        SnowflakeUtil.nextId(),
                JsonUtil.objectToString(map), 365, TimeUnit.DAYS);
    }
}
